{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:34:11.131361Z",
     "start_time": "2025-02-12T10:34:10.540197Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ],
   "id": "22df9e34b6d12d49",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:34:17.101429Z",
     "start_time": "2025-02-12T10:34:16.257753Z"
    }
   },
   "cell_type": "code",
   "source": [
    "userSub = pd.read_csv('userSub.csv',usecols = ['user_id','item_id','behavior_type','time'],parse_dates = True)\n",
    "userSub"
   ],
   "id": "1b935fa866b5590c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          user_id    item_id  behavior_type           time\n",
       "0        10001082  275221686              1  2014-12-03 01\n",
       "1        10001082   97441652              1  2014-11-20 21\n",
       "2        10001082  275221686              1  2014-12-13 14\n",
       "3        10001082  275221686              1  2014-12-08 07\n",
       "4        10001082  125083630              1  2014-12-14 03\n",
       "...           ...        ...            ...            ...\n",
       "2769024  65341491  264469913              1  2014-12-08 19\n",
       "2769025  65341491  191375871              1  2014-12-08 19\n",
       "2769026  65341491  133486908              1  2014-12-08 19\n",
       "2769027  65341491  242501625              1  2014-12-08 19\n",
       "2769028  65341491  133486908              1  2014-12-08 19\n",
       "\n",
       "[2769029 rows x 4 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>behavior_type</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-03 01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10001082</td>\n",
       "      <td>97441652</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-11-20 21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-13 14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-08 07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10001082</td>\n",
       "      <td>125083630</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-14 03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2769024</th>\n",
       "      <td>65341491</td>\n",
       "      <td>264469913</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-08 19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2769025</th>\n",
       "      <td>65341491</td>\n",
       "      <td>191375871</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-08 19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2769026</th>\n",
       "      <td>65341491</td>\n",
       "      <td>133486908</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-08 19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2769027</th>\n",
       "      <td>65341491</td>\n",
       "      <td>242501625</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-08 19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2769028</th>\n",
       "      <td>65341491</td>\n",
       "      <td>133486908</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-08 19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2769029 rows × 4 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:37:59.273272Z",
     "start_time": "2025-02-12T10:37:59.242355Z"
    }
   },
   "cell_type": "code",
   "source": "userSub.info()",
   "id": "3a4cab335b718bb0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 2769029 entries, 0 to 2769028\n",
      "Data columns (total 4 columns):\n",
      " #   Column         Dtype \n",
      "---  ------         ----- \n",
      " 0   user_id        int64 \n",
      " 1   item_id        int64 \n",
      " 2   behavior_type  int64 \n",
      " 3   time           object\n",
      "dtypes: int64(3), object(1)\n",
      "memory usage: 84.5+ MB\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:35:58.453874Z",
     "start_time": "2025-02-12T10:35:58.397930Z"
    }
   },
   "cell_type": "code",
   "source": "%time userSub = userSub.sort_index().copy()",
   "id": "7f40e667bb42000b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 62.5 ms\n",
      "Wall time: 51.7 ms\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:36:03.989991Z",
     "start_time": "2025-02-12T10:36:03.986037Z"
    }
   },
   "cell_type": "code",
   "source": "userSub.index",
   "id": "ef24fbfb9ed03585",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=2769029, step=1)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:36:20.607135Z",
     "start_time": "2025-02-12T10:36:20.600782Z"
    }
   },
   "cell_type": "code",
   "source": "userSub.head()",
   "id": "3eb98fe97950ee7d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    user_id    item_id  behavior_type           time\n",
       "0  10001082  275221686              1  2014-12-03 01\n",
       "1  10001082   97441652              1  2014-11-20 21\n",
       "2  10001082  275221686              1  2014-12-13 14\n",
       "3  10001082  275221686              1  2014-12-08 07\n",
       "4  10001082  125083630              1  2014-12-14 03"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>behavior_type</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-03 01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10001082</td>\n",
       "      <td>97441652</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-11-20 21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-13 14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-08 07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10001082</td>\n",
       "      <td>125083630</td>\n",
       "      <td>1</td>\n",
       "      <td>2014-12-14 03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:36:36.008252Z",
     "start_time": "2025-02-12T10:36:35.951865Z"
    }
   },
   "cell_type": "code",
   "source": "pd.get_dummies(userSub['behavior_type'],prefix = 'type').head()",
   "id": "545abc0c812520ff",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   type_1  type_2  type_3  type_4\n",
       "0    True   False   False   False\n",
       "1    True   False   False   False\n",
       "2    True   False   False   False\n",
       "3    True   False   False   False\n",
       "4    True   False   False   False"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type_1</th>\n",
       "      <th>type_2</th>\n",
       "      <th>type_3</th>\n",
       "      <th>type_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:36:52.670508Z",
     "start_time": "2025-02-12T10:36:52.562872Z"
    }
   },
   "cell_type": "code",
   "source": [
    "typeDummies = pd.get_dummies(userSub['behavior_type'],prefix = 'type')#onehot哑变量编码\n",
    "\n",
    "userSubOneHot = pd.concat([userSub[['user_id','item_id','time']],typeDummies],axis = 1)"
   ],
   "id": "3965f05303ca83aa",
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:36:57.101117Z",
     "start_time": "2025-02-12T10:36:57.034230Z"
    }
   },
   "cell_type": "code",
   "source": "usertem = pd.concat([userSub[['user_id','item_id']],typeDummies,userSub[['time']]],axis = 1)#将哑变量特征加入数据表中",
   "id": "5cb611e5e4810f25",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:37:07.030279Z",
     "start_time": "2025-02-12T10:37:07.023860Z"
    }
   },
   "cell_type": "code",
   "source": "usertem.head()",
   "id": "93a14016d985db76",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    user_id    item_id  type_1  type_2  type_3  type_4           time\n",
       "0  10001082  275221686    True   False   False   False  2014-12-03 01\n",
       "1  10001082   97441652    True   False   False   False  2014-11-20 21\n",
       "2  10001082  275221686    True   False   False   False  2014-12-13 14\n",
       "3  10001082  275221686    True   False   False   False  2014-12-08 07\n",
       "4  10001082  125083630    True   False   False   False  2014-12-14 03"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>type_1</th>\n",
       "      <th>type_2</th>\n",
       "      <th>type_3</th>\n",
       "      <th>type_4</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>2014-12-03 01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10001082</td>\n",
       "      <td>97441652</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>2014-11-20 21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>2014-12-13 14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>2014-12-08 07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10001082</td>\n",
       "      <td>125083630</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>2014-12-14 03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:38:52.778544Z",
     "start_time": "2025-02-12T10:38:51.975647Z"
    }
   },
   "cell_type": "code",
   "source": "usertem.groupby(['time','user_id','item_id'],as_index = False).sum()#已将关键字排序，统计用户商品对的交互行为",
   "id": "110429c083eb247",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                 time    user_id    item_id  type_1  type_2  type_3  type_4\n",
       "0       2014-11-18 00    1409053   58649567       1       0       0       0\n",
       "1       2014-11-18 00    1446949    2432119       1       0       0       0\n",
       "2       2014-11-18 00    1446949  206833072       1       0       0       0\n",
       "3       2014-11-18 00    1446949  347745633       1       0       0       0\n",
       "4       2014-11-18 00    2903578  395200199       1       0       0       0\n",
       "...               ...        ...        ...     ...     ...     ...     ...\n",
       "968238  2014-12-18 23  142014633  400526861       1       0       0       0\n",
       "968239  2014-12-18 23  142282362  247238488       4       0       0       0\n",
       "968240  2014-12-18 23  142381953  104613625       2       0       0       0\n",
       "968241  2014-12-18 23  142381953  132738480       2       0       0       0\n",
       "968242  2014-12-18 23  142381953  210978116       1       0       0       0\n",
       "\n",
       "[968243 rows x 7 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>type_1</th>\n",
       "      <th>type_2</th>\n",
       "      <th>type_3</th>\n",
       "      <th>type_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1409053</td>\n",
       "      <td>58649567</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1446949</td>\n",
       "      <td>2432119</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1446949</td>\n",
       "      <td>206833072</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1446949</td>\n",
       "      <td>347745633</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>2903578</td>\n",
       "      <td>395200199</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>968238</th>\n",
       "      <td>2014-12-18 23</td>\n",
       "      <td>142014633</td>\n",
       "      <td>400526861</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>968239</th>\n",
       "      <td>2014-12-18 23</td>\n",
       "      <td>142282362</td>\n",
       "      <td>247238488</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>968240</th>\n",
       "      <td>2014-12-18 23</td>\n",
       "      <td>142381953</td>\n",
       "      <td>104613625</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>968241</th>\n",
       "      <td>2014-12-18 23</td>\n",
       "      <td>142381953</td>\n",
       "      <td>132738480</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>968242</th>\n",
       "      <td>2014-12-18 23</td>\n",
       "      <td>142381953</td>\n",
       "      <td>210978116</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>968243 rows × 7 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:39:24.267136Z",
     "start_time": "2025-02-12T10:39:24.260087Z"
    }
   },
   "cell_type": "code",
   "source": "userSubOneHot.head()",
   "id": "f6386c909924f7cd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    user_id    item_id           time  type_1  type_2  type_3  type_4\n",
       "0  10001082  275221686  2014-12-03 01    True   False   False   False\n",
       "1  10001082   97441652  2014-11-20 21    True   False   False   False\n",
       "2  10001082  275221686  2014-12-13 14    True   False   False   False\n",
       "3  10001082  275221686  2014-12-08 07    True   False   False   False\n",
       "4  10001082  125083630  2014-12-14 03    True   False   False   False"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>time</th>\n",
       "      <th>type_1</th>\n",
       "      <th>type_2</th>\n",
       "      <th>type_3</th>\n",
       "      <th>type_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>2014-12-03 01</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10001082</td>\n",
       "      <td>97441652</td>\n",
       "      <td>2014-11-20 21</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>2014-12-13 14</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10001082</td>\n",
       "      <td>275221686</td>\n",
       "      <td>2014-12-08 07</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10001082</td>\n",
       "      <td>125083630</td>\n",
       "      <td>2014-12-14 03</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:39:42.900006Z",
     "start_time": "2025-02-12T10:39:42.100663Z"
    }
   },
   "cell_type": "code",
   "source": "userSubOneHotGroup = userSubOneHot.groupby(['time','user_id','item_id'],as_index = False).sum()#另外一种方法是在sum（）后使用.reset_index()",
   "id": "737d0cc70a3de73d",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:39:50.673010Z",
     "start_time": "2025-02-12T10:39:50.632333Z"
    }
   },
   "cell_type": "code",
   "source": "userSubOneHotGroup.info()",
   "id": "256b9dafcc65a00d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 968243 entries, 0 to 968242\n",
      "Data columns (total 7 columns):\n",
      " #   Column   Non-Null Count   Dtype \n",
      "---  ------   --------------   ----- \n",
      " 0   time     968243 non-null  object\n",
      " 1   user_id  968243 non-null  int64 \n",
      " 2   item_id  968243 non-null  int64 \n",
      " 3   type_1   968243 non-null  int64 \n",
      " 4   type_2   968243 non-null  int64 \n",
      " 5   type_3   968243 non-null  int64 \n",
      " 6   type_4   968243 non-null  int64 \n",
      "dtypes: int64(6), object(1)\n",
      "memory usage: 51.7+ MB\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:40:00.255607Z",
     "start_time": "2025-02-12T10:40:00.248620Z"
    }
   },
   "cell_type": "code",
   "source": "userSubOneHotGroup.head()",
   "id": "97bf8fe0c0e8e295",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            time  user_id    item_id  type_1  type_2  type_3  type_4\n",
       "0  2014-11-18 00  1409053   58649567       1       0       0       0\n",
       "1  2014-11-18 00  1446949    2432119       1       0       0       0\n",
       "2  2014-11-18 00  1446949  206833072       1       0       0       0\n",
       "3  2014-11-18 00  1446949  347745633       1       0       0       0\n",
       "4  2014-11-18 00  2903578  395200199       1       0       0       0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>type_1</th>\n",
       "      <th>type_2</th>\n",
       "      <th>type_3</th>\n",
       "      <th>type_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1409053</td>\n",
       "      <td>58649567</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1446949</td>\n",
       "      <td>2432119</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1446949</td>\n",
       "      <td>206833072</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1446949</td>\n",
       "      <td>347745633</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>2903578</td>\n",
       "      <td>395200199</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:40:11.448940Z",
     "start_time": "2025-02-12T10:40:10.894567Z"
    }
   },
   "cell_type": "code",
   "source": [
    "userSubOneHotGroup['time_day'] = pd.to_datetime(userSubOneHotGroup.time.values).date\n",
    "\n",
    "userSubOneHotGroup['time_hour'] = pd.to_datetime(userSubOneHotGroup.time.values).time\n",
    "\n",
    "userSubOneHotGroup.head()"
   ],
   "id": "b41762a2894d7b25",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            time  user_id    item_id  type_1  type_2  type_3  type_4  \\\n",
       "0  2014-11-18 00  1409053   58649567       1       0       0       0   \n",
       "1  2014-11-18 00  1446949    2432119       1       0       0       0   \n",
       "2  2014-11-18 00  1446949  206833072       1       0       0       0   \n",
       "3  2014-11-18 00  1446949  347745633       1       0       0       0   \n",
       "4  2014-11-18 00  2903578  395200199       1       0       0       0   \n",
       "\n",
       "     time_day time_hour  \n",
       "0  2014-11-18  00:00:00  \n",
       "1  2014-11-18  00:00:00  \n",
       "2  2014-11-18  00:00:00  \n",
       "3  2014-11-18  00:00:00  \n",
       "4  2014-11-18  00:00:00  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>type_1</th>\n",
       "      <th>type_2</th>\n",
       "      <th>type_3</th>\n",
       "      <th>type_4</th>\n",
       "      <th>time_day</th>\n",
       "      <th>time_hour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1409053</td>\n",
       "      <td>58649567</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1446949</td>\n",
       "      <td>2432119</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1446949</td>\n",
       "      <td>206833072</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>1446949</td>\n",
       "      <td>347745633</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014-11-18 00</td>\n",
       "      <td>2903578</td>\n",
       "      <td>395200199</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>00:00:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:42:02.513830Z",
     "start_time": "2025-02-12T10:42:02.480112Z"
    }
   },
   "cell_type": "code",
   "source": "dataHour = userSubOneHotGroup.iloc[:,0:7]",
   "id": "2a84ee91e9a02ceb",
   "outputs": [],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:42:20.851111Z",
     "start_time": "2025-02-12T10:42:20.812637Z"
    }
   },
   "cell_type": "code",
   "source": "dataHour.info()",
   "id": "775c07cff363ad9e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 968243 entries, 0 to 968242\n",
      "Data columns (total 7 columns):\n",
      " #   Column   Non-Null Count   Dtype \n",
      "---  ------   --------------   ----- \n",
      " 0   time     968243 non-null  object\n",
      " 1   user_id  968243 non-null  int64 \n",
      " 2   item_id  968243 non-null  int64 \n",
      " 3   type_1   968243 non-null  int64 \n",
      " 4   type_2   968243 non-null  int64 \n",
      " 5   type_3   968243 non-null  int64 \n",
      " 6   type_4   968243 non-null  int64 \n",
      "dtypes: int64(6), object(1)\n",
      "memory usage: 51.7+ MB\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:42:40.037351Z",
     "start_time": "2025-02-12T10:42:38.567975Z"
    }
   },
   "cell_type": "code",
   "source": "dataHour.to_csv('dataHour.csv')",
   "id": "f93b4fff3e97015e",
   "outputs": [],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:42:46.722088Z",
     "start_time": "2025-02-12T10:42:46.456470Z"
    }
   },
   "cell_type": "code",
   "source": "dataHour.duplicated().sum()#没有重复行",
   "id": "5125304d26383e7a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:46:38.082119Z",
     "start_time": "2025-02-12T10:46:37.590013Z"
    }
   },
   "cell_type": "code",
   "source": [
    "numeric_columns = userSubOneHotGroup.select_dtypes(include='number').columns\n",
    "dataDay = userSubOneHotGroup.groupby(['time_day', 'user_id', 'item_id'], as_index=False)[numeric_columns].sum()"
   ],
   "id": "6e6bbbacc7b661dc",
   "outputs": [],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:46:39.855012Z",
     "start_time": "2025-02-12T10:46:39.808586Z"
    }
   },
   "cell_type": "code",
   "source": "dataDay.info()",
   "id": "2145084d8c2da55f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 904397 entries, 0 to 904396\n",
      "Data columns (total 7 columns):\n",
      " #   Column    Non-Null Count   Dtype \n",
      "---  ------    --------------   ----- \n",
      " 0   time_day  904397 non-null  object\n",
      " 1   user_id   904397 non-null  int64 \n",
      " 2   item_id   904397 non-null  int64 \n",
      " 3   type_1    904397 non-null  int64 \n",
      " 4   type_2    904397 non-null  int64 \n",
      " 5   type_3    904397 non-null  int64 \n",
      " 6   type_4    904397 non-null  int64 \n",
      "dtypes: int64(6), object(1)\n",
      "memory usage: 48.3+ MB\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:46:59.478269Z",
     "start_time": "2025-02-12T10:46:59.469224Z"
    }
   },
   "cell_type": "code",
   "source": "dataDay.head()",
   "id": "2f86261cf246f5be",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     time_day  user_id    item_id  type_1  type_2  type_3  type_4\n",
       "0  2014-11-18      492   76093985       1       0       0       0\n",
       "1  2014-11-18      492  110036513       1       0       0       0\n",
       "2  2014-11-18      492  176404510       1       0       0       0\n",
       "3  2014-11-18      492  178412255       1       0       0       0\n",
       "4  2014-11-18      492  335961429       1       0       0       0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time_day</th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>type_1</th>\n",
       "      <th>type_2</th>\n",
       "      <th>type_3</th>\n",
       "      <th>type_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>492</td>\n",
       "      <td>76093985</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>492</td>\n",
       "      <td>110036513</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>492</td>\n",
       "      <td>176404510</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>492</td>\n",
       "      <td>178412255</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014-11-18</td>\n",
       "      <td>492</td>\n",
       "      <td>335961429</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:47:14.344392Z",
     "start_time": "2025-02-12T10:47:12.732106Z"
    }
   },
   "cell_type": "code",
   "source": "dataDay.to_csv('dataDay.csv')",
   "id": "1faee5e559809352",
   "outputs": [],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:47:20.635602Z",
     "start_time": "2025-02-12T10:47:20.389548Z"
    }
   },
   "cell_type": "code",
   "source": "dataDay.duplicated().sum()#没有重复行",
   "id": "ca6be18985a0dc70",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T10:47:26.510445Z",
     "start_time": "2025-02-12T10:47:26.503043Z"
    }
   },
   "cell_type": "code",
   "source": "dataDay.type_4.max()",
   "id": "8157d672e9e98d27",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3412"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 46
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
